package com.yuntongxun.ecdemo.voicechat.ui.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.loopj.android.http.RequestParams;
import com.yuntongxun.ecdemo.R;
import com.yuntongxun.ecdemo.common.dialog.ECAlertDialog;
import com.yuntongxun.ecdemo.common.dialog.ECProgressDialog;
import com.yuntongxun.ecdemo.voicechat.api.ApiConstants;
import com.yuntongxun.ecdemo.voicechat.bean.PostResult;
import com.yuntongxun.ecdemo.voicechat.comman.EventBusTags;
import com.yuntongxun.ecdemo.voicechat.presenter.ICommonRequestPresenter;
import com.yuntongxun.ecdemo.voicechat.presenter.impl.CommonRequestPresenterImpl;
import com.yuntongxun.ecdemo.voicechat.util.iview.ICommonViewUi;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * 充值
 *
 * @author li_zhuonan
 * @version 1.0
 * @since 2016/11/21
 */

public class TopUpActivity extends BaseChatActivity implements ICommonViewUi {


    @InjectView(R.id.coins_count)
    TextView coinsCountTv;
    @InjectView(R.id.coins1)
    Button coins1Btn;
    @InjectView(R.id.coins2)
    Button coins2Btn;
    @InjectView(R.id.coins3)
    Button coins3Btn;
    @InjectView(R.id.coins4)
    Button coins4Btn;
    @InjectView(R.id.coins5)
    Button coins5Btn;
    @InjectView(R.id.other)
    Button otherBtn;
    @InjectView(R.id.buy_coins_count)
    TextView buyCoinsCountTV;
    @InjectView(R.id.next_btn)
    Button nextBtn;

    String coins="";

    private ICommonRequestPresenter iCommonRequestPresenter;

    @Override
    protected boolean isBindEventBusHere() {
        return true;
    }

    public void onEvent(PostResult postResult){
        if (EventBusTags.PAY_SUCCESS.equals(postResult.getTag())){
            toRequest(ApiConstants.EventTags.BEGIN_EVENT);
        }
    }


    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_top_up;
    }

    @Override
    protected void initViewsAndEvents() {
        coins=coins1Btn.getText().toString().trim();
        setTitle("Top up");
        iCommonRequestPresenter = new CommonRequestPresenterImpl(mContext, this);
        toRequest(ApiConstants.EventTags.BEGIN_EVENT);
    }

    @OnClick({R.id.coins1,R.id.coins2,R.id.coins3,R.id.coins4,R.id.coins5,R.id.other,R.id.next_btn})
    public void onClick(View view){

        switch (view.getId()){
            case R.id.coins1:
                setDefaulBg();
                coins1Btn.setBackgroundResource(R.drawable.ic_top_up_select_bg);
                buyCoinsCountTV.setText("Buy "+coins1Btn.getText().toString().trim()+" coins");
                coins=coins1Btn.getText().toString().trim();
                break;
            case R.id.coins2:
                setDefaulBg();
                coins2Btn.setBackgroundResource(R.drawable.ic_top_up_select_bg);
                buyCoinsCountTV.setText("Buy "+coins2Btn.getText().toString().trim()+" coins");
                coins=coins2Btn.getText().toString().trim();
                break;
            case R.id.coins3:
                setDefaulBg();
                coins3Btn.setBackgroundResource(R.drawable.ic_top_up_select_bg);
                buyCoinsCountTV.setText("Buy "+coins3Btn.getText().toString().trim()+" coins");
                coins=coins3Btn.getText().toString().trim();
                break;
            case R.id.coins4:
                setDefaulBg();
                coins4Btn.setBackgroundResource(R.drawable.ic_top_up_select_bg);
                buyCoinsCountTV.setText("Buy "+coins4Btn.getText().toString().trim()+" coins");
                coins=coins4Btn.getText().toString().trim();
                break;
            case R.id.coins5:
                setDefaulBg();
                coins5Btn.setBackgroundResource(R.drawable.ic_top_up_select_bg);
                buyCoinsCountTV.setText("Buy "+coins5Btn.getText().toString().trim()+" coins");
                coins=coins5Btn.getText().toString().trim();
                break;
            case R.id.other:
                showDialog();
                break;
            case R.id.next_btn:
                Intent intent=new Intent(this, PaymentActivity.class);
                intent.putExtra(PaymentActivity.PAY_COINS,coins);
                startActivity(intent);
                break;
        }
    }

    ECAlertDialog buildAlert;
    private void showDialog() {
        final EditText coinsEdt=new EditText(this);
        coinsEdt.setInputType(InputType.TYPE_CLASS_NUMBER);
        coinsEdt.setFilters(new InputFilter[]{new InputFilter.LengthFilter(5)});
        coinsEdt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().startsWith("0")) {
                    coinsEdt.setText(s.replace(0,1,""));
                }
            }
        });
        buildAlert = ECAlertDialog.buildAlert(mContext, "Are you sure you want to upload to the web?",
                "Cancel", "Submit", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        setDefaulBg();
                        otherBtn.setBackgroundResource(R.drawable.ic_top_up_select_bg);
                        otherBtn.setText(coinsEdt.getText().toString().trim());
                        buyCoinsCountTV.setText("To Buy "+coinsEdt.getText().toString().trim()+" coins");
                        coins=coinsEdt.getText().toString().trim();
                    }
                });
        buildAlert.setTitle("Please input quantity");
        buildAlert.setContentView(coinsEdt);
        buildAlert.setCanceledOnTouchOutside(false);
        buildAlert.show();
    }

    private void setDefaulBg(){
        coins1Btn.setBackgroundResource(R.drawable.ic_top_up_normal_bg);
        coins2Btn.setBackgroundResource(R.drawable.ic_top_up_normal_bg);
        coins3Btn.setBackgroundResource(R.drawable.ic_top_up_normal_bg);
        coins4Btn.setBackgroundResource(R.drawable.ic_top_up_normal_bg);
        coins5Btn.setBackgroundResource(R.drawable.ic_top_up_normal_bg);
        otherBtn.setBackgroundResource(R.drawable.ic_top_up_normal_bg);
    }


    @Override
    public void toRequest(int eventTag) {
        RequestParams map = new RequestParams();
        iCommonRequestPresenter.request(eventTag, mContext, ApiConstants.Urls.ACCOUNT, map);
    }

    @Override
    public void getRequestData(int eventTag, String result) {
        try {
            JSONObject object=new JSONObject(result);
            String account =object.getString("result");
            coinsCountTv.setText(account);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onRequestSuccessException(int eventTag, String msg) {
        showToastLong(msg);
        finish();
    }

    @Override
    public void onRequestFailureException(int eventTag, String msg) {
        showToastLong(msg);
        finish();
    }

    private ECProgressDialog mPostingdialog;
    @Override
    public void isRequesting(int eventTag, boolean status) {
        if(status){
            mPostingdialog = new ECProgressDialog(this, "Waiting");
            mPostingdialog.show();
        }else{
            mPostingdialog.dismiss();
        }
    }
}
